package com.monkeygroup.mygoplus.ecchat.ui.contact;

import android.app.Dialog;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.monkeygroup.mygoplus.MyGoApplication;
import com.monkeygroup.mygoplus.R;
import com.monkeygroup.mygoplus.base.BaseActivity;
import com.monkeygroup.mygoplus.common.DefinedView.MyDialog;
import com.monkeygroup.mygoplus.common.utils.VilidateUtil;
import com.monkeygroup.mygoplus.ecchat.common.utils.ToastUtil;
import com.monkeygroup.mygoplus.ecchat.ui.contact.adapter.ContactApplyAdapter;
import com.monkeygroup.mygoplus.ecchat.ui.contact.entity.ContactApplyDTO;
import com.monkeygroup.mygoplus.http.HttpModel;
import com.monkeygroup.mygoplus.http.RequestUrl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/5/21.
 */
public class NewFriendApplyActivity extends BaseActivity implements View.OnClickListener {
    private ListView new_apply_listview;
    private ContactApplyAdapter adapter;
    private List<ContactApplyDTO> applyList = new ArrayList<>();
    private Map<String, String> mParams = new HashMap<>();
    private Dialog applyDialog = null;
    private final String TAG = "NewFriendApplyActivity";

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_new_applylist);
    }

    @Override
    public void initView() {
        new_apply_listview = (ListView) findViewById(R.id.new_apply_listview);
    }

    @Override
    public void setListener() {
        findViewById(R.id.layout_back).setOnClickListener(this);
        new_apply_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                applyDialog = new MyDialog(NewFriendApplyActivity.this, R.style.MyDialogTheme, null, new MyDialog.OnNewFriendApplyListener() {
                    @Override
                    public void doAgree(String userid) {
                        applyDialog.dismiss();

                        mParams.clear();
                        mParams.put("mobile", MyGoApplication.myECUserid);
                        mParams.put("friendMobile", adapter.getItem(position).getUserid());
                        doApplyEvent(true);
                    }

                    @Override
                    public void doRefuse(String userid) {
                        applyDialog.dismiss();

                        mParams.clear();
                        mParams.put("mobile", MyGoApplication.myECUserid);
                        mParams.put("friendMobile", adapter.getItem(position).getUserid());
                        doApplyEvent(false);
                    }
                });
                applyDialog.show();
            }
        });
    }

    @Override
    public void doEvent() {
        for (int i = 0; i < 5; i++) {
            ContactApplyDTO model = new ContactApplyDTO();
            applyList.add(model);
        }

        initAdapter();

        doApplyData();
    }

    private void initAdapter() {
        adapter = new ContactApplyAdapter(this, applyList);
        new_apply_listview.setAdapter(adapter);
    }

    private void setFriendsApplyData(List<Map<String, String>> list) {
        Map<String, ContactApplyDTO> mylist = new HashMap<>();
        List<ContactApplyDTO> itemList = new ArrayList<>();
        for (Map<String, String> map : list) {
            if (!mylist.containsKey(map.get("mobile"))) {
                ContactApplyDTO item = new ContactApplyDTO();
                item.setHeadicon(map.get("headIcon"));
                item.setNickname(map.get("userID"));
                item.setUserid(map.get("mobile"));
                mylist.put(map.get("mobile"), item);

                itemList.add(item);
            }
        }

        applyList.clear();
        applyList.addAll(itemList);
        adapter.notifyDataSetChanged();
    }

    /**
     * 獲取好友申請列表數據
     **/
    private void doApplyData() {
        String requestUrl = RequestUrl.baseUrl + RequestUrl.requestApplyList;
        HttpModel httpModel = new HttpModel(requestUrl, getParams(), new HttpModel.SimpleOnHttpRequestListener() {
            @Override
            public void onRequestFinished(int resultCode, String resultMsg, Object data) {
                if (VilidateUtil.isSuccess(resultCode)) {
                    Map<String, Object> map = (Map) data;
                    List<Map<String, String>> list = (ArrayList) map.get("list");
                    setFriendsApplyData(list);
                } else {
                    ToastUtil.showMessage(resultMsg);
                }
            }
        }, true);

        httpModel.startRequest();
    }

    private Map<String, String> getParams() {
        Map<String, String> params = new HashMap<>();
        params.put("mobile", MyGoApplication.myECUserid);

        return params;
    }

    /**
     * 獲取好友申請列表數據
     **/
    private void doApplyEvent(boolean isAdded) {
        if (isAdded) {
            mParams.put("isAgreeAdd", "true");
        } else {
            mParams.put("isAgreeAdd", "false");
        }

        String requestUrl = RequestUrl.baseUrl + RequestUrl.requestAgreeFriend;
        HttpModel httpModel = new HttpModel(requestUrl, mParams, new HttpModel.SimpleOnHttpRequestListener() {
            @Override
            public void onRequestFinished(int resultCode, String resultMsg, Object data) {
                if (VilidateUtil.isSuccess(resultCode)) {
                    Map<String, String> map = (Map) data;
                    ToastUtil.showMessage(map.get("message"));
                } else {
                    ToastUtil.showMessage(resultMsg);
                }
            }
        }, true);

        httpModel.startRequest();
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.layout_back:
                finish();
                break;
            default:
                break;
        }
    }
}
